﻿Type=StaticCode
Version=1.90
@EndOfDesignText@
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
Dim stringutils1 As StringUtils
End Sub
Sub Globals
	Dim SV As ScrollView
	Dim Header As Panel
	Dim Table As Panel
	Dim NumberOfColumns, RowHeight, RowHeight_1, ColumnWidth, ColumnWidth_1 As Int
	Dim HeaderColor, LineColor, CellColor, FontColor, HeaderFontColor As Int
	Dim ColLineWidth, RowLineWidth As Int
	Dim FontSize As Float
	Type RowCol (Row As Int, Col As Int)
	Dim Alignment As Int
	Dim SelectedRow As Int
	Dim SelectedRowColor As Int		: SelectedRowColor=Colors.LightGray
	
	'Table settings
	HeaderColor = Colors.Gray
	NumberOfColumns = 4 'will be overwritten when loading from CSV file.
	ColLineWidth = 1dip
	RowLineWidth = 1dip
	RowHeight_1 = 30dip
	RowHeight=RowHeight_1+RowLineWidth
	LineColor = Colors.Black
	CellColor = Colors.White
	FontColor = Colors.Black
	HeaderFontColor = Colors.White
	FontSize = 14
	Alignment = Gravity.CENTER 'change to Gravity.LEFT or Gravity.RIGHT for other alignments.

End Sub

Sub Cell_Click
	Dim rc As RowCol
	Dim l As Label
	l = Sender
	rc = l.Tag
	SelectRow(rc.Row)
	'Activity.Title = "Cell clicked: (" & rc.Row & ", " & rc.Col & ")"
End Sub

Sub Header_Click
	Dim l As Label
	Dim col As Int
	l = Sender
	col = l.Tag
	'Activity.Title = "Header clicked: " & col
End Sub

Sub SelectRow(Row As Int)
	'remove the color of previously selected row
	If SelectedRow > -1 Then
		For col = 0 To NumberOfColumns - 1
			GetView(SelectedRow, col).Color = CellColor
		Next
	End If
	SelectedRow = Row
	For col = 0 To NumberOfColumns - 1
		GetView(Row, col).Color = SelectedRowColor
	Next
End Sub

'Returns the label in the specific cell
Sub GetView(Row As Int, Col As Int) As Label
	Dim l As Label
	l = Table.GetView(Row * NumberOfColumns + Col)
	Return l
End Sub

'Adds a row to the table
Sub AddRow(Values() As String)
	If Values.Length <> NumberOfColumns Then
		Log("Wrong number of values.")
		Return
	End If
	Dim lastRow As Int
	lastRow = NumberOfRows
	For i = 0 To NumberOfColumns - 1
		Dim l As Label
		l.Initialize("cell")
		l.Text = Values(i)
		l.Gravity = Alignment
		l.TextSize = FontSize
		l.TextColor = FontColor
		l.Color=Colors.White
		Dim rc As RowCol
		rc.Initialize
		rc.Col = i
		rc.Row = lastRow
		l.Tag = rc
		Table.AddView(l, ColumnWidth * i, RowHeight * lastRow, ColumnWidth_1, RowHeight_1)
	Next
	Table.Height = NumberOfRows * RowHeight
End Sub

'Set the headers values
Sub SetHeader(Values() As String)
	If Header.IsInitialized Then Return 'should only be called once
	Header.Initialize("")
	For i = 0 To NumberOfColumns - 1
		Dim l As Label
		l.Initialize("header")
		l.Text = Values(i)
		l.Gravity = Gravity.CENTER
		l.TextSize = FontSize
		l.Color = HeaderColor
		l.TextColor = HeaderFontColor
		l.Tag = i
		Header.AddView(l, ColumnWidth * i, 0, ColumnWidth_1, RowHeight_1)
	Next
	Activity.AddView(Header, SV.Left, SV.Top - RowHeight, SV.Width, RowHeight)
End Sub

Sub NumberOfRows As Int
	Return Table.NumberOfViews / NumberOfColumns
End Sub

'Sets the value of the given cell
Sub SetCell(Row As Int, Col As Int, Value As String)
	GetView(Row, Col).Text = Value
End Sub

'Gets the value of the given cell
Sub GetCell(Row As Int, Col As Int) As String
	Return GetView(Row, Col).Text
End Sub

'Clears the table
Sub ClearAll
	For i = Table.NumberOfViews -1 To 0 Step -1
		Table.RemoveViewAt(i)
	Next
	Table.Height = 0
	SelectedRow = -1
End Sub